<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Admin\Service;
/**
 * @todo 区域服务层
 * @author PHP@妖孽 <dino_ma@163.com>
 * @since 2015-9-3
 */
class AreaService{
	
	 	protected $error	 =   '';
	
	 	/**
	 	 * @todo 获取省份list
	 	 * @author PHP@妖孽<dino_ma@163.com>
	 	 */
	 	public function getProvinceList(){
	 		//在增加省份的时候将base_province_list 置成Null   S('base_province_list',null);
	 		if(S('base_province_list')){
	 			$list= S('base_province_list');
	 		}else{
	 			$list = M('province')->select();
	 			S('base_province_list',$list);
	 		}
	 		return $this->callBack($list);
	 	}
	 	
	 	/**
	 	 * @todo 获取省份下的城市
	 	 * @param int province_id 省份id
	 	 * @author PHP@妖孽<dino_ma@163.com>
	 	 */
	 	public function getProvinceCityList($province_id){
	 		//在增加城市的时候将base_city_list 置成Null S('base_city_list',null);
	 		$province_id = (int)$province_id;
	 		if($province_id ==0){
	 			$this->error='省份Id有问题';
	 			return false;
	 		}
	 		$city = S('base_city_list');
	 		if(!empty($city[$province_id])){
	 			$list= $city[$province_id];
	 		}else{
	 			$list = M('city')->where(array('province_id'=>$province_id,'status'=>1))->select();
	 			S('base_city_list',array($province_id=>$list));
	 		}
	 		return $this->callBack($list);
	 	}
	 	
	 	/**
	 	 * @todo 获取城市下的行政区
	 	 * @param int city_id 城市id
	 	 * @author PHP@妖孽<dino_ma@163.com>
	 	 */
	 	public function getCityWard($city_id){
	 		//在增加行政区的时候将base_ward_list 置成Null  S('base_ward_list',null);
	 		$city_id = (int)$city_id;
	 		if($city_id ==0){
	 			$this->error='城市Id有问题';
	 			return false;
	 		}
	 		$ward = S('base_ward_list');
	 		if(!empty($ward[$city_id])){
	 			$list =  $ward[$city_id];
	 		}else{
	 			$list = M('ward')->where(array('city_id'=>$city_id,'status'=>1))->select();
	 			S('base_ward_list',array($city_id=>$list));
	 		}
	 		return $this->callBack($list);
	 	}
	 	
	 	/**
	 	 * @todo 获取行政区下的小区
	 	 * @param int ward_id 行政区id
	 	 * @author PHP@妖孽<dino_ma@163.com>
	 	 */
	 	public function getWardVillage($ward_id){
	 		//在增加小区的时候将base_village_list 置成Null S('base_village_list',null);
	 		$ward_id = (int)$ward_id;
	 		if($ward_id ==0){
	 			$this->error = '行政区Id有问题';
	 			return false;
	 		}
	 		$ward = S('base_village_list');
	 		if(!empty($ward[$ward_id])){
	 			$list = $ward[$ward_id];
	 		}else{
	 			$list = M('village')->where(array('ward_id'=>$ward_id,'status'=>1))->select();
	 			S('base_village_list',array($ward_id=>$list));
	 		}
	 		return $this->callBack($list);
	 	}
		
	 	
	 	/**
	 	 * @todo 返回数据
	 	 * @param array data
	 	 * return
	 	 * <li>status(int)</li>1-成功,0-失败
	 	 * <li>info(string)</li>错误提示信息
	 	 * <li>data(array) </li>数据
	 	 */
	 	private function callBack($data){
	 		static $call_back =array();
	 		if(empty($data)){
	 			$call_back['status']=0;
	 			$call_back['info'] = $this->getError();
	 		}else{
	 			$call_back['status']=1;
	 			$call_back['data'] = $data;
	 		}
	 		return $call_back;
	 	}
	 	
		/**
		 * @todo 返回最后一次错误记录
		 * @return string
		 * @author PHP@妖孽 <dino_ma@163.com>
		 */
		public function getError(){
			return $this->error;
		}
}
